#!/bin/bash

#set up a wallet just for holding he  key used during blockchain igniion

bioshost=$BIOS_HOSTNAME
if [ -z "$bioshost" ]; then
   bioshost=localhost
fi

biosport=$BIOS_HTTP_PORT
if [ -z "$biosport" ]; then
    biosport=9776
fi

wddir=eosio-ignition-wd
wdport=8899
wdhost=127.0.0.1
# Manual deployers, add a line below this block that looks like:
#    bioshost=$BIOS_HOSTNAME
#    biosport=$BIOS_HTTP_PORT
#    cnt_dir=$CONTRACT_DIR
#    sys=$SYS_CONTRACT
# where $BIOS_HOSTNAME is replaced by the hostname portion of the http-server-address configured
# for the nodeos instance running to bios producer
# ------ DO NOT ALTER THE NEXT LINE -------
###INSERT envars

logfile=$wddir/bootlog.txt

if [ -e $wddir ]; then
    rm -rf $wddir
fi
mkdir $wddir

step=1
echo Initializing ignition sequence  at $(date) | tee $logfile

echo "http-server-address = 127.0.0.1:$wdport" > $wddir/config.ini

programs/eosiowd/eosiowd --config-dir $wddir --data-dir $wddir 2> $wddir/wdlog.txt &
echo $$ > ignition_wallet.pid
echo eosiowd log in $wddir/wdlog.txt >> $logfile
sleep 1

ecmd () {
    echo ===== Start: $step ============ >> $logfile
    echo executing: cleos --wallet-port $wdport -p $biosport  -H $bioshost $* | tee -a $logfile
    echo ----------------------- >> $logfile
    programs/cleos/cleos --wallet-port $wdport --wallet-host $wdhost -p $biosport  -H $bioshost $* >> $logfile 2>&1
    echo ==== End: $step ============== >> $logfile
    step=$(($step + 1))
}

wcmd () {
    ecmd wallet $*
}

cacmd () {
    ecmd create account eosio $*
}

sleep 2
ecmd get info


wcmd create -n ignition
# Manual deployers, add a line below this block that looks like:
#    wcmd import -n ignition $BIOSKEY
# where $BIOSKEY is replaced by the private key for the bios node
# ------ DO NOT ALTER THE NEXT LINE -------
###INSERT bioskey

# Manual deployers, add a series of lines below this block that looks like:
#    cacmd $PRODNAME[0] $DWNERKEY[0] $ACTIVEKEY[0]
#    cacmd $PRODNAME[1] $OWNERKEY[1] $ACTOVEKEY[1]
#    ...
#    cacmd $PRODNAME[20] $OWNERKEY[20] $ACTIVEKEY[20]
# where $PRODNAME is the producer account name and $OWNERKEY and $ACTIVEKEY are both the producer's
# public key
# ------ DO NOT ALTER THE NEXT LINE -------
###INSERT cacmd


ecmd set contract eosio contracts/test.system/test.system.wast contracts/test.system/test.system.abi

#the setprods.json argument cannot pass through the function call due to embedded quotes
echo ===== Start: $step ============ >> $logfile
echo executing: cleos --wallet-port $wdport -p $biosport -H $bioshost push action eosio setprods "$(cat setprods.json)" -p eosio@active | tee -a $logfile
echo ----------------------- >> $logfile
programs/cleos/cleos --wallet-port $wdport --wallet-host $wdhost -p $biosport -H $bioshost push action eosio setprods "$(cat setprods.json)" -p eosio@active >> $logfile 2>&1
echo ==== End: $step ============== >> $logfile
step=$(($step + 1))

ecmd set contract eosio contracts/eosio.system/eosio.system.wast contracts/eosio.system/eosio.system.abi

echo ===== Start: $step ============ >> $logfile
echo executing: cleos --wallet-port $wdport -p $biosport -H $bioshost push action eosio issue '{"to":"eosio","quantity":"1000000000.0000 EOS","memo":"init"}' -p eosio@active | tee -a $logfile
echo ----------------------- >> $logfile
programs/cleos/cleos --wallet-port $wdport --wallet-host $wdhost -p $biosport -H $bioshost push action eosio issue '{"to":"eosio","quantity":"1000000000.0000 EOS","memo":"init"}' -p eosio@active >> $logfile 2>&1
echo ==== End: $step ============== >> $logfile
step=$(($step + 1))

for a in a b c d e f g h i j k l m n o p q r s t u; do ecmd transfer eosio init$a 1000000.0000 "fund producer init$a"; done


echo waiting for producers to settle
sleep 15
echo shutting down ignition wallet and bios node
pkill -15 eosiowd
kill -15 $(cat var/lib/node_bios/nodeos.pid) && rm var/lib/node_bios/nodeos.pid
